<template>
  <div class="container">
    <div class="content">
      <h2>管理登录-登录</h2>
      <el-form class="form">
        <el-form-item label="">
          <el-input v-model="formData.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formData.password"
            type="password"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="formData.captcha" placeholder="验证码">
            <template #append>
              <div
                class="captcha-img"
                v-html="imgHtml"
                @click="getCaptcha"
              ></div>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" style="width: 100%" @click="login"
        >登陆</el-button
      >
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "Login",
  data() {
    return {
      formData: {
        username: "",
        password: "",
        captcha: "",
      },
      imgHtml: "",
      captchaText: "",
    };
  },
  created() {
    this.getCaptcha();
  },
  methods: {
    getCaptcha() {
      axios.get("/users/backend/code").then(
        (res) => {
          this.imgHtml = res.data.data.replace('height="38"', 'height="36"').replace('viewBox="0,0,80,38"', 'viewBox="0,0,80,36"');
          this.captchaText = res.data.text;
        },
        (err) => {
          this.$message.error(err);
        }
      );
    },
    login() {
      axios.post("/users/backend/login", this.formData).then(
        (res) => {
          if (!this.formData.captcha)
            return this.$message.error("请输入验证码！");
          if (
            this.formData.captcha.toLocaleLowerCase() !=
            this.captchaText.toLocaleLowerCase()
          ) {
            return this.$message.error("验证码不正确！");
          }
          this.$message.success(res.message);
          localStorage.setItem("authorization", res.token);
          localStorage.setItem("user", JSON.stringify(res.user));
          this.$router.replace("/home");
        },
        (err) => {
          this.$message.error(err);
        }
      );
    },
  },
};
</script>
<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('@/assets/login.gif'); /* 添加背景图片 */
  background-size: cover; /* 背景图片铺满容器 */
  background-position: center; /* 居中显示背景图片 */
  background-color: rgba(0, 0, 0, 0.4); /* 使用rgba来设置背景颜色及透明度 */
}

.content {
  text-align: center;
  background-color: rgb(00, 00, 00, 0.4);
  padding: 0 20px 20px 20px;
  width: 280px;
  color: #fff;

  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); /* 添加阴影效果 */
}

.captcha-img {
  display: inline-block;
  width: 100px;
}

:deep(.el-input-group__append) {
  padding: 0px;
  border: none;
}

@media (max-width: 480px) {
  .content {
    width: 90%; /* 在小屏幕下适应宽度 */
  }
}
</style>
